﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VisualExamEngine
{
    class clsUtilities
    {
        /// <summary>
        /// DateTime: 5/8/15 9:30 PM
        /// Author: Mike Hartwig
        /// Purpose: Get a letter at a specified index
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static string GetLetter(int index)
        {
            string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            return alpha.Substring(index, 1);
        }

        /// <summary>
        /// DateTime: 5/12/15 9:30 PM
        /// Author: Mike Hartwig
        /// Purpose: Move the selected listbox item up or down.
        /// </summary>
        /// <param name="direction"></param>
        /// <param name="lb"></param>
        public static void MoveItem(int direction, System.Windows.Forms.ListBox lb)
        {
            // Checking selected item
            if (lb.SelectedItem == null || lb.SelectedIndex < 0)
                return; // No selected item - nothing to do

            // Calculate new index using move direction
            int newIndex = lb.SelectedIndex + direction;

            // Checking bounds of the range
            if (newIndex < 0 || newIndex >= lb.Items.Count)
                return; // Index out of range - nothing to do

            object selected = lb.SelectedItem;

            // Removing removable element
            lb.Items.Remove(selected);

            // Insert it in new position
            lb.Items.Insert(newIndex, selected);
            
            // Restore selection
            lb.SetSelected(newIndex, true);
        }
    }
}
